//
//  SINASettingAlertPhongInputViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2017/12/27.
//  Copyright © 2017年  androidlongs. All rights reserved.
//

#import "SINASettingAlertPhongInputViewController.h"

@interface SINASettingAlertPhongInputViewController ()<UITextFieldDelegate>
//提交数据 按钮
@property (weak, nonatomic) IBOutlet UIButton *commitAlertPhoneButton;
@property (weak, nonatomic) IBOutlet UITextField *inputAlertPhoneField;

@end

@implementation SINASettingAlertPhongInputViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor colorWithHexString:@"#eeeeee"]];
    self.navigationItem.title=@"修改手机号";
    //设置lable的圆角显示
    self.commitAlertPhoneButton.layer.cornerRadius =5;
    //设置lable的边框 宽度为1
    self.commitAlertPhoneButton.layer.borderWidth=0;
    //设置lable的边框颜色
    self.commitAlertPhoneButton.layer.borderColor=[UIColor redColor].CGColor;
    //设置 显示大小范围为 border内容范围中
    self.commitAlertPhoneButton.clipsToBounds = YES;
    [self.commitAlertPhoneButton setBackgroundColor: [UIColor colorWithRed:0/255 green:198.0/255 blue:174.0/255 alpha:0.8f]];
    
    self.inputAlertPhoneField.delegate=self;
    self.inputAlertPhoneField .keyboardType = UIKeyboardTypeNumberPad;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//修改用户手机号
- (IBAction)alertPhoneClickFunction:(UIButton *)sender {
    
    NSString *alertPhone = self.inputAlertPhoneField.text;
    
    if (alertPhone==nil||[alertPhone isEqualToString:@""]) {
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"请输入手机号" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
        return;
    }else if ([alertPhone length]!=11){
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"请输入11位手机号" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
        return;
    }else if ([[[SINACommonUserHelper share]getUserModel].userPhone isEqualToString:alertPhone]){
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"请输入与原来不同的手机号" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
        return;
    }
    [self alertUserPhone:alertPhone];
}



//修改
-(void) alertUserPhone:(NSString *) userPhone{
    //登录请求参数
    
    SINAUserModel *userModel =[[SINACommonUserHelper share]getUserModel];
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    
    [dict setObject:userModel.userUid forKey:@"uuid"];
    [dict setObject:userPhone forKey:@"phone"];
    
    NSMutableDictionary *dict2 = [[NSMutableDictionary alloc]init];
    
    [dict2 setObject:[SINAHttpRequestUtils encodeDicParams:dict] forKey:@"sj"];

    [self startLoading];
    
    WeakSelf;
    [CXAFNHttpRequestUtils httpPostRequest:[NSString stringWithFormat:@"%@%@",baseUrl,userAlertInformationUrl] withParamters:dict2 success:^(id responseData) {
        
        [weakSelf stopLoading];
        
        NSString *code = responseData[@"code"];
        if ([code isEqualToString:@"1000"]) {
            //            NSDictionary *result = responseData[@"content"];
//            weakSelf.loadingView.text=@"修改成功";
            [YPXToastView showToastViewWithText:@"修改成功" andDuration:2 andCorner:5 andParentView:weakSelf.view];
            userModel.userPhone = userPhone;
            [[SINACommonUserHelper share]saveUserModel:userModel];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
                [weakSelf stopLoading];
                [weakSelf.navigationController popViewControllerAnimated:YES];
            });
            
            
            
            
        }else if ([code isEqualToString:@"1003"]) {
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"验证失败" sureBtn:@"确定" cancleBtn:nil];
            [alertView showXLAlertView];
        }else{
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定" cancleBtn:nil];
            [alertView showXLAlertView];
        }
        
        
        
    } failure:^(NSError *error, NSInteger errCode) {
    
        [weakSelf stopLoading];
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"验证失败" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
    }];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == self.inputAlertPhoneField) {
        if ([textField.text length] > 10) {
            textField.text = [textField.text substringWithRange:NSMakeRange(0, 10)];
            [textField.undoManager removeAllActions];
            [textField becomeFirstResponder];
        }
    }
    return YES;
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
